using System;
using System.Drawing;
using FireStarterModeller.BO;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// This control is used for editing the Fields of a <see cref="DMClass"/>. Typically
    ///  the fields being edited are the ClassName, Description etc.
    /// </summary>
    public class DMClassEditorControl : UserControlWin, IBOEditorControl
    {
        private readonly ITextBox _businessObjectNameTextBox;
        private readonly ITextBox _displayNameTextBox;
        private IDMSolution _solution;
//        private readonly IButton _createUIDefButton;
//        private readonly IButton _editUIDefinitionButton;

        public IControlFactory ControlFactory { get; private set; }
        public IPanelInfo DetailPanelInfo { get; private set; }
        public SuperClassControl SuperClassControl { get; private set; }

        public DMClassEditorControl(IControlFactory controlFactory)
        {
            ControlFactory = controlFactory;
            IGroupBox classDetailsGroupBox = SetupClassDetailsGroupBox();
            _businessObjectNameTextBox = (ITextBox)DetailPanelInfo.FieldInfos["ClassNameBO"].InputControl;
            _displayNameTextBox = (ITextBox)DetailPanelInfo.FieldInfos["DisplayName"].InputControl;
            
            IGroupBox superClassGroupBox = SetupSuperClassGroupBox();
            
            IPanel panel = ControlFactory.CreatePanel();
            panel.Height = classDetailsGroupBox.Height + 30;
            panel.Width = classDetailsGroupBox.Width + 10 + superClassGroupBox.Width;
            GridLayoutManager layoutManager = new GridLayoutManager(panel, ControlFactory);
            layoutManager.SetGridSize(1, 2);
            layoutManager.FixColumnBasedOnContents(0);
            layoutManager.AddControl(classDetailsGroupBox);
            layoutManager.AddControl(superClassGroupBox);
              //Issue #396
//            IButtonGroupControl buttonGroupControl = ControlFactory.CreateButtonGroupControl();
//            buttonGroupControl.Width = 130;
//            buttonGroupControl.Height = 80;
//            _createUIDefButton = buttonGroupControl.AddButton("CreateUIDef", "Create UI Definition", CreateUIDef_ClickHandler);
//            _createUIDefButton.Enabled = false;
//            _editUIDefinitionButton = buttonGroupControl.AddButton("EditUIDef", "Edit UI Definition", EditUIDefinition_ClickHandler);
//            _editUIDefinitionButton.Enabled = false;
            FlowLayoutManager columnLayoutManager = new FlowLayoutManager(this, ControlFactory);
            columnLayoutManager.AddControl(panel);
//            columnLayoutManager.AddControl(buttonGroupControl);
            Height = panel.Height;// +10;
        }

        private IGroupBox SetupSuperClassGroupBox()
        {
            IGroupBox superClassGroupBox = ControlFactory.CreateGroupBox();
            SuperClassControl = new SuperClassControl(ControlFactory);
            superClassGroupBox.Size = System.Drawing.Size.Add(SuperClassControl.Size, new Size(10, 0));
            SuperClassControl.Dock = (System.Windows.Forms.DockStyle) DockStyle.Fill;
            superClassGroupBox.Controls.Add(SuperClassControl);
            return superClassGroupBox;
        }

        private IGroupBox SetupClassDetailsGroupBox()
        {
            PanelBuilder panelBuilder = new PanelBuilder(ControlFactory);
            DetailPanelInfo = panelBuilder.BuildPanelForForm(ClassDef.Get<DMClass>().UIDefCol["default"].UIForm);
            IPanel boPanel = DetailPanelInfo.Panel;
            IGroupBox groupBox = ControlFactory.CreateGroupBox();
            boPanel.Dock = DockStyle.Fill;
            groupBox.Controls.Add(boPanel);
            //groupBox.Dock = DockStyle.Left;
            const int detailPanelWidth = 400;
            boPanel.Width = detailPanelWidth;
            groupBox.Height = 190;
            groupBox.Width = detailPanelWidth + 10;
            return groupBox;
        }

        public ITextBox DisplayNameTextBox
        {
            get { return _displayNameTextBox; }
        }

        public ITextBox BusinessObjectNameTextBox
        {
            get { return _businessObjectNameTextBox; }
        }

        #region IBOEditorControl Members

        public IBusinessObject BusinessObject
        {
            get { return DetailPanelInfo.BusinessObject; }
            set
            {
                RelationshipComboBoxMapper controlMapper =
                    (RelationshipComboBoxMapper) DetailPanelInfo.FieldInfos["Assembly"].ControlMapper;
                DMClass dmClass = (DMClass) value;
                IDMSolution solution = null;
                if (dmClass != null)
                {
                    solution = dmClass.Solution;
                }
                if (solution != null)
                {
                    if (!ReferenceEquals(_solution, solution))
                    {
                        _solution = solution;
                        controlMapper.IncludeBlankItem = false;
                        controlMapper.BusinessObjectCollection = _solution.Assemblies;
                    }
                }
                DetailPanelInfo.BusinessObject = value;
//                if (dmClass == null)
//                {
//                    this.CreateUIDefButton.Enabled = false;
//                    this.EditUIDefinitionButton.Enabled = false;
//                } else
//                {
//                    this.CreateUIDefButton.Enabled = true;
//                    this.EditUIDefinitionButton.Enabled = dmClass.UIViews.Count > 0;
//                }
               
                SuperClassControl.SubClass = dmClass;
            }
        }

        #endregion

     /*   private void EditUIDefinition_ClickHandler(object sender, EventArgs args) { 
            UIViewsEditor.EditUIViews((DMClass) BusinessObject);
        }

        private void CreateUIDef_ClickHandler(object sender, EventArgs e)
        {
            try
            {
                DMClass dmClass = ((DMClass) BusinessObject);
                if (dmClass == null) return;
                dmClass.Save();
                CreateViewWizardController wizardController = new CreateViewWizardController
                          {
                              Solution = (DMSolution) dmClass.Solution,
                              Class = dmClass
                          };
                IWizardForm wizardForm = ControlFactory.CreateWizardForm(wizardController);
                wizardForm.Text = "Select Properties for the Grid";
                wizardForm.Height = 600;
                wizardForm.Width = 620;
                wizardForm.ShowDialog();
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }*/

        public void DisplayErrors()
        {
            try
            {
                DetailPanelInfo.ApplyChangesToBusinessObject();
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        public void ClearErrors()
        {
            try
            {
                DetailPanelInfo.ClearErrorProviders();
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        #region Implementation of IBOEditorControl

        /// <summary>
        ///             Applies any changes that have occured in any of the Controls on this control's to their related
        ///             Properties on the Business Object.
        /// </summary>
        public void ApplyChangesToBusinessObject()
        {
            DetailPanelInfo.ApplyChangesToBusinessObject();
        }

        /// <summary>
        ///             Does the business object controlled by this control or any of its Aggregate or Composite children have and Errors.
        /// </summary>
        public bool HasErrors
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Does the Business Object controlled by this control or any of its Aggregate or Composite children have and warnings.
        /// </summary>
        public bool HasWarning
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///              Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        public ErrorList Errors
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Does the business object being managed by this control have any edits that have not been persisted.
        /// </summary>
        /// <returns>
        /// </returns>
        public bool IsDirty
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        /// <returns>
        /// </returns>
        public ErrorList Warnings
        {
            get { throw new NotImplementedException(); }
        }

        /*public IButton CreateUIDefButton
        {
            get { return _createUIDefButton; }
        }

        public IButton EditUIDefinitionButton
        {
            get { return _editUIDefinitionButton; }
        }
*/
        public IUIViewsEditor UIViewsEditor { get; set; }

        #endregion


        //        public IPanelInfo SuperClassPanelInfo
        //        {
        //            get { return _superClassPanelInfo; }
        //        }

        //public ITextBox MappedTableNameTextBox
        //{
        //    get { return _mappedTableNameTextBox; }
        //}
    }
}
